package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.AdminStorageListVo;
import com.cskaoyan.bean.vo.StorageCreateData;
import com.cskaoyan.service.StorageService;
import com.cskaoyan.utils.StaticFilePath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author Fyy（完成创建） And xujitao(完成其他查改删)
 * @since 2022/12/28 22:26
 */

@RestController
@RequestMapping("admin/storage")
public class StorageController {
    @Autowired
    StorageService storageService;

    /**
     * 图片上传
     *
     * @param file 接收客户端上传的文件
     * @return
     */
    @RequestMapping("create")
    public BaseRespVo create(MultipartFile file) {
        //获得上传文件名
        String originalFilename = file.getOriginalFilename();
        //获得文件大小
        long size = file.getSize();
        //获取uuid
        String uuid = UUID.randomUUID().toString();
        //本地图片名
        String[] split = originalFilename.split("\\.");
        String filename = uuid + "." +split[1];
        File files = new File(StaticFilePath.getStaticPath(), filename);
        try {
            file.transferTo(files);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //在服务器中访问图片的url
        String url = StaticFilePath.getPicUrl(filename);
        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setAddTime(new Date());
        marketStorage.setKey(filename);
        marketStorage.setName(originalFilename);
        marketStorage.setSize((int) size);
        marketStorage.setType("image/jpeg");
        marketStorage.setUpdateTime(new Date());
        marketStorage.setUrl(url);
        storageService.create(marketStorage);
        //给返回值data对象赋值
        StorageCreateData data = StorageCreateData.data(marketStorage);
        BaseRespVo respVo = BaseRespVo.ok(data);
        respVo.setErrmsg("成功");
        return respVo;
    }


    /**
     * 完成对象存储中的查询
     *
     * @return 对象信息
     */
    @RequestMapping("list")
    public AdminStorageListVo list(BasePageInfo info, String key, String name) {

        CommonData<MarketStorage> data = storageService.getList(info, key, name);
        return AdminStorageListVo.ok(data);
    }

    /**
     *  删除对象的功能
     * @param marketStorage
     * @return
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketStorage marketStorage){
        storageService.deleteMarketStorage(marketStorage);
        return BaseRespVo.ok();
    }

    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketStorage marketStorage){
        //修改一下修改的时间
        marketStorage.setUpdateTime(new Date());
        //进行修改
        storageService.updateMarketStorage(marketStorage);
        return  BaseRespVo.ok(marketStorage,"成功");
    }

}
